using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace WL.Think.Abp.HealthChecks;

/// <summary>
/// 健康检查基类
/// </summary>
public abstract class HealthCheckBase : IHealthCheck
{
    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        try
        {
            var isHealthy = await IsHealthyAsync(context, cancellationToken);
            if (isHealthy)
            {
                return HealthCheckResult.Healthy("The check indicates a healthy result.");
            }
            else
            {
                return HealthCheckResult.Unhealthy("The check indicates an unhealthy result.");
            }
        }
        catch (Exception ex)
        {
            return HealthCheckResult.Unhealthy("The check indicates an unhealthy result.", ex);
        }
    }

    protected abstract Task<bool> IsHealthyAsync(HealthCheckContext context, CancellationToken cancellationToken = default);
}
